﻿Imports System.Data.OleDb
Public Class LikePhones
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        img_Smiley.Visible = False
        'this will make sure user can like only one phone at a time
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("Phones").ConnectionString)
        Dim Sql = "SELECT* From UserLikes WHERE Reader = @f1 AND PhoneID = @F2"
        Dim oleDbCmd As OleDbCommand = New OleDbCommand(Sql, oleDbConn)
        oleDbConn.Open()
        oleDbCmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        oleDbCmd.Parameters.AddWithValue("@f2", Request.Params("ID"))
        Dim dr = oleDbCmd.ExecuteReader()
        If dr.HasRows Then
            img_Smiley.Visible = True
            bt_Like.Visible = False
        Else
            'THIS WILL HIDE THE IMAGE AND SHOW THE BUTTON
            img_Smiley.Visible = False
            bt_Like.Visible = True
        End If
    End Sub


    Protected Sub bt_Like_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bt_Like.Click
        img_Smiley.Visible = True
        bt_Like.Visible = False
        'inserting users like into the database
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("Phones").ConnectionString)
        Dim Sql = "Insert into UserLikes(PhoneID,Reader) Values (@f1,@f2)"
        Dim oleDbCmd As OleDbCommand = New OleDbCommand(Sql, oleDbConn)
        oleDbCmd.CommandType = CommandType.Text
        oleDbConn.Open()
        oleDbCmd.Parameters.AddWithValue("@f1", Request.Params("ID"))
        oleDbCmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        oleDbCmd.ExecuteNonQuery()
        oleDbConn.Close()
        oleDbConn.Dispose()
        lb_Likes.Text = "Thank you for Liking this"
    End Sub
End Class